package handler

import (
	"git.oschina.net/fanbuchi/redpack/constant"
	"git.oschina.net/fanbuchi/redpack/domain"
	"git.oschina.net/fanbuchi/redpack/errors"
	"git.oschina.net/fanbuchi/redpack/service"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"net/http"
	"strconv"
)


type RedPackHandler struct {
	*service.RedPackService
}


// @Summary 创建红包
// @Description 创建一个红包
// @Tags redpack
// @Accept  json
// @Produce  json
// @Param   redpack     body    domain.RedPackReq     true        "红包信息"
// @Param   Authorization     header    string     true        "权限认证" default(Bearer 3bbea1b6-f8e4-4da9-a9f3-0413abff47c5)
// @Success 200 {object} domain.RedPack    "调用成功"
// @Failure 400 {object} domain.Rsp "失败，参数错误!"
// @Router /redpack/create [post]
func  (handler*RedPackHandler)Create(c *gin.Context)  {
	var params domain.RedPack
	if err := c.ShouldBindBodyWith(&params, binding.JSON); err != nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}
	accountId:=c.GetInt64(constant.AccountId)
	params.AccountId=accountId

	result,err:=handler.RedPackService.Create(&params)
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusForbidden,Msg:err.Error()})
		return
	}
	c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusOK,Data:result})
}




// @Summary 删除一个红包
// @Description 删除一个红包
// @Tags redpack
// @Accept  json
// @Produce  json
// @Param    id    query    int     true        "红包id" default(1)
// @Param   Authorization     header    string     true        "权限认证" default(Bearer 3bbea1b6-f8e4-4da9-a9f3-0413abff47c5)
// @Success 200 {object} domain.RedPack    "调用成功"
// @Failure 400 {object} domain.Rsp "失败，参数错误!"
// @Router /redpack/delete [delete]
func  (handler*RedPackHandler)Delete(c *gin.Context)  {
	var id int64
	if err := c.ShouldBindQuery(&id);err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}
	accountId:=c.GetInt64(constant.AccountId)
	accountRole:=c.GetString(constant.AccountRole)
	//普通用戶不能删除其他人的信息
	if accountRole!=domain.ROLE_ADMIN{
		id=accountId
	}
	_,err:=handler.RedPackService.Delete1ById(id)
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusForbidden,Msg:err.Error()})
		return
	}
	//密码
	c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusOK,Msg:"红包已删除"})
}

// @Summary 查询红包
// @Description QueryById 查询一个红包
// @Tags redpack
// @Accept  json
// @Produce  json
// @Param    id    query    int     true        "账户id" default(1)
// @Param   Authorization     header    string     true        "权限认证" default(Basic Y2xpZW50OnNlY3JldA==)
// @Success 200 {object} domain.RedPack    "调用成功"
// @Failure 400 {object} domain.Rsp "失败，参数错误!"
// @Router /redpack/query [get]
func  (handler*RedPackHandler)QueryById(c *gin.Context)  {
	id,has := c.GetQuery("id")
	params,err:=strconv.ParseInt(id,10,64)
	if !has {
		c.JSON(http.StatusOK, domain.Rsp{Status: http.StatusBadRequest, Msg: http.StatusText(http.StatusBadRequest)})
		return
	}

	result,err:=handler.RedPackService.Query1ById(params)

	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusForbidden,Msg:err.Error()})
		return
	}
	if result==nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusForbidden,Msg:_error.ErrAccountNotExist.Error()})
		return
	}
	c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusOK,Data:result})
}




// @Summary 查询红包列表
// @Description search 查询红包列表
// @Tags redpack
// @Accept  json
// @Produce  json
// @Param   redpack     body    domain.RedPackReq     true        "查询结构条件"
// @Param   page     query    int     true        "page" default(1)
// @Param   size     query    int     true        "size" default(2)
// @Param   Authorization     header    string     true        "权限认证" default(Basic Y2xpZW50OnNlY3JldA==)
// @Success 200 {object} domain.RedPack    "调用成功"
// @Failure 400 {object} domain.Rsp "失败，参数错误!"
// @Router /redpack/search [post]
func  (handler*RedPackHandler)Search(c *gin.Context)  {
	page:= c.DefaultQuery("page","1")
	size:= c.DefaultQuery("size","18")
	//params,err:=strconv.ParseInt(id,10,64)
	var req domain.RedPackReq
	err := c.ShouldBindBodyWith(&req,binding.JSON)
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}
	params:=domain.RedPack{ID:req.ID,
		Category:req.Category,
		AccountId:req.AccountId,
		Amount:req.Amount,
		Fee:req.Fee,
		Latitude:req.Latitude,
		Longitude:req.Longitude,
		TargetId:req.TargetId}

	_page,_:=strconv.Atoi(page)
	_size,_:=strconv.Atoi(size)
	ret,err:=handler.RedPackService.Search(&params,_page,_size)
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}
	c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusOK,Data:ret})
}